<!DOCTYPE html>
<html lang="en">

<head>
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
	<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js" integrity="sha384-alpBpkh1PFOepccYVYDB4do5UnbKysX5WZXm3XxPqe5iKTfUKjNkCk9SaVuEZflJ"
	 crossorigin="anonymous"></script>
	<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb"
	 crossorigin="anonymous">
	<script type="text/javascript">




		var getDate = function (y, m) {
			var s, e;
			if (m == "01") { s = y + "-01-01"; e = y + "-01-31" }
			if (m == "02") { s = y + "-02-01"; e = y + "-02-28" }
			if (m == "03") { s = y + "-03-01"; e = y + "-03-31" }
			if (m == "04") { s = y + "-04-01"; e = y + "-04-30" }
			if (m == "05") { s = y + "-05-01"; e = y + "-05-31" }
			if (m == "06") { s = y + "-06-01"; e = y + "-06-30" }
			if (m == "07") { s = y + "-07-01"; e = y + "-07-31" }
			if (m == "08") { s = y + "-08-01"; e = y + "-08-31" }
			if (m == "09") { s = y + "-09-01"; e = y + "-09-30" }
			if (m == "10") { s = y + "-10-01"; e = y + "-10-31" }
			if (m == "11") { s = y + "-11-01"; e = y + "-11-30" }
			if (m == "12") { s = y + "-12-01"; e = y + "-12-31" }
			return [s, e];
		};

		var url = window.location.href;
		var arrayUrl = url.split('=');
		var ou = arrayUrl[3];
		var arrayUrl1 = arrayUrl[2].split('&');
		var pe = arrayUrl1[0];
		var year = pe.substring(0, 4);
		var month = pe.substring(4, 6);
		var prog_id = "VCuHIFtJJSv";
		var dates = getDate(year, month);
		var startdate = dates[0];
		var enddate = dates[1];
		var totalevent, totalEndevent = 0, totalEndevent2 = 0, activeatendevent;
		var t1countn = 0, t1countp = 0;
		var t2countn = 0, t2countp = 0;
		var it1countn = 0, it1countp = 0;
		var it2countn = 0, it2countp = 0;

		var checkStatus = function (data) {
			data.enrollments.forEach(function (element, index) {
				var tei = element.trackedEntityInstance;


				$.when(
					$.getJSON("../api/events.json?orgUnit=" + ou + "&program=" + prog_id + "&programStage=Kr60c8j7vMe&trackedEntityInstance=" + tei + "&skipPaging=true&endDate=" + enddate, {
						format: "json"
					}),
					$.getJSON("../api/events.json?orgUnit=" + ou + "&program=" + prog_id + "&trackedEntityInstance=" + tei + "&orderBy=eventDate:ASC&skipPaging=true", {
						format: "json"
					})

				).then(function (data1, data2) {
					var count = 0, count2 = 0, count22 = 0, count3 = 0, flag = 0, flag2 = 0;
					var type1p = false, type1n = false;
					var type2p = false, type2n = false;
					var itype1p = false, itype1n = false;
					var itype2p = false, itype2n = false;

					if (data2[0].events.length == 0) {
						count2 = 0;
					}
					else if (data2.httpStatusCode == 409 || data2.httpStatusCode == "409") { count2 = 0; }
					else {
						for (var b = 0; b < data2[0].events.length; b++) {
							var date = data2[0].events[b].eventDate;
							var first = date.split('T')[0];
							var expireDate = new Date(first);
							if (new Date(enddate) >= expireDate && expireDate >= new Date(startdate)) {
								var currentEventAttr = data2[0].events[b].dataValues;
								for (var j = 0; j < currentEventAttr.length; j++) {
									if ((currentEventAttr[j].dataElement == "lzBg6QalyhT" || currentEventAttr[j].dataElement == "rwDJebu16Fu") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
										type1p = true;
										for (var p = 0; p < currentEventAttr.length; p++) {
											if (currentEventAttr[p].value == "Insulin, 100 IU/ml, Biphasic 30, human, 10 ml" || currentEventAttr[p].value == "Insuline NPH" || currentEventAttr[p].value == "Insulin rapid" || currentEventAttr[p].value == "Insulin regular") {
												itype1p = true;
											}
										}
									}
									if ((currentEventAttr[j].dataElement == "uoVoakOJULl" || currentEventAttr[j].dataElement == "nwFajZjl3Fa") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
										type2p = true;
										for (var p = 0; p < currentEventAttr.length; p++) {
											if (currentEventAttr[p].value == "Insulin, 100 IU/ml, Biphasic 30, human, 10 ml" || currentEventAttr[p].value == "Insuline NPH" || currentEventAttr[p].value == "Insulin rapid" || currentEventAttr[p].value == "Insulin regular") {
												itype2p = true;
											}
										}
									}
								}
								// for the first visit case

								if (b == 0) {
									for (var j = 0; j < currentEventAttr.length; j++) {
										if ((currentEventAttr[j].dataElement == "lzBg6QalyhT") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
											type1n = true;
											for (var p = 0; p < currentEventAttr.length; p++) {
												if (currentEventAttr[p].value == "Insulin, 100 IU/ml, Biphasic 30, human, 10 ml" || currentEventAttr[p].value == "Insuline NPH" || currentEventAttr[p].value == "Insulin rapid" || currentEventAttr[p].value == "Insulin regular") {
													itype1n = true;
												}
											}
										}
										if ((currentEventAttr[j].dataElement == "uoVoakOJULl") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
											type2n = true;
											for (var p = 0; p < currentEventAttr.length; p++) {
												if (currentEventAttr[p].value == "Insulin, 100 IU/ml, Biphasic 30, human, 10 ml" || currentEventAttr[p].value == "Insuline NPH" || currentEventAttr[p].value == "Insulin rapid" || currentEventAttr[p].value == "Insulin regular") {
													itype2n = true;
												}
											}
										}
									}
								}

								// for the follow up visit case
								else {
									if (!type1n) {
										for (var j = 0; j < currentEventAttr.length; j++) {
											if ((currentEventAttr[j].dataElement == "rwDJebu16Fu") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
												type1n = true;
												for (var p = 0; p < currentEventAttr.length; p++) {
													if (currentEventAttr[p].value == "Insulin, 100 IU/ml, Biphasic 30, human, 10 ml" || currentEventAttr[p].value == "Insuline NPH" || currentEventAttr[p].value == "Insulin rapid" || currentEventAttr[p].value == "Insulin regular") {
														itype1n = true;
													}
												}
											}
										}
									}
									if (!type2n) {
										for (var j = 0; j < currentEventAttr.length; j++) {
											if ((currentEventAttr[j].dataElement == "nwFajZjl3Fa") && (currentEventAttr[j].value == "Newly_diagnosed" || currentEventAttr[j].value == "Previously_diagnosed")) {
												type2n = true;
												for (var p = 0; p < currentEventAttr.length; p++) {
													if (currentEventAttr[p].value == "Insulin, 100 IU/ml, Biphasic 30, human, 10 ml" || currentEventAttr[p].value == "Insuline NPH" || currentEventAttr[p].value == "Insulin rapid" || currentEventAttr[p].value == "Insulin regular") {
														itype2n = true;
													}
												}
											}
										}
									}
								}


							}
						}
						if (type1n) { t1countn++; }
						if (type2n) { t2countn++; }
						if (itype1n) { it1countn++; }
						if (itype2n) { it2countn++; }
						if (type1p) { t1countp++; }
						if (type2p) { t2countp++; }
						if (itype1p) { it1countp++; }
						if (itype2p) { it2countp++; }
					}



					document.getElementById('col1').innerHTML = it1countp;
					document.getElementById('col2').innerHTML = t1countp;
					document.getElementById('col3').innerHTML = it2countp;
					document.getElementById('col4').innerHTML = t2countp;
					document.getElementById('col5').innerHTML = it1countn;
					document.getElementById('col6').innerHTML = t1countn;
					document.getElementById('col7').innerHTML = it2countn;
					document.getElementById('col8').innerHTML = t2countn;
					document.getElementById('p').innerHTML = index + " of " + (data.enrollments.length - 1) + " completed";
					if (data.enrollments.length - 1 == index) {
						document.getElementById('loader').style.display = "none";
					}
				});

			});

		};

		$(document).ready(function () {

			$.when(
				$.getJSON("../api/enrollments.json?ou=" + ou + "&ouMode=DESCENDANTS&program=" + prog_id + "&fields=trackedEntityInstance&skipPaging=true&programEndDate=" + enddate, {
					format: "json"
				})
			).then(function (data1) {
				checkStatus(data1);

			});
		});
	</script>
	<style>
		table {
			table-layout: fixed;
			word-wrap: break-word;
			text-align: center;
		}

		/* Absolute Center Spinner */

		.loading {
			position: fixed;
			z-index: 999;
			height: 2em;
			width: 2em;
			overflow: show;
			margin: auto;
			top: 0;
			left: 0;
			bottom: 0;
			right: 0;
		}

		/* Transparent Overlay */

		.loading:before {
			content: '';
			display: block;
			position: fixed;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
			background-color: rgba(0, 0, 0, 0.3);
		}

		/* :not(:required) hides these rules from IE9 and below */

		.loading:not(:required) {
			/* hide "loading..." text */
			font: 0/0 a;
			color: transparent;
			text-shadow: none;
			background-color: transparent;
			border: 0;
		}

		.loading:not(:required):after {
			content: '';
			display: block;
			font-size: 10px;
			width: 1em;
			height: 1em;
			margin-top: -0.5em;
			-webkit-animation: spinner 1500ms infinite linear;
			-moz-animation: spinner 1500ms infinite linear;
			-ms-animation: spinner 1500ms infinite linear;
			-o-animation: spinner 1500ms infinite linear;
			animation: spinner 1500ms infinite linear;
			border-radius: 0.5em;
			-webkit-box-shadow: rgba(0, 0, 0, 0.75) 1.5em 0 0 0, rgba(0, 0, 0, 0.75) 1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) 0 1.5em 0 0, rgba(0, 0, 0, 0.75) -1.1em 1.1em 0 0, rgba(0, 0, 0, 0.5) -1.5em 0 0 0, rgba(0, 0, 0, 0.5) -1.1em -1.1em 0 0, rgba(0, 0, 0, 0.75) 0 -1.5em 0 0, rgba(0, 0, 0, 0.75) 1.1em -1.1em 0 0;
			box-shadow: rgba(0, 0, 0, 0.75) 1.5em 0 0 0, rgba(0, 0, 0, 0.75) 1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) 0 1.5em 0 0, rgba(0, 0, 0, 0.75) -1.1em 1.1em 0 0, rgba(0, 0, 0, 0.75) -1.5em 0 0 0, rgba(0, 0, 0, 0.75) -1.1em -1.1em 0 0, rgba(0, 0, 0, 0.75) 0 -1.5em 0 0, rgba(0, 0, 0, 0.75) 1.1em -1.1em 0 0;
		}

		/* Animation */

		@-webkit-keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		@-moz-keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		@-o-keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		@keyframes spinner {
			0% {
				-webkit-transform: rotate(0deg);
				-moz-transform: rotate(0deg);
				-ms-transform: rotate(0deg);
				-o-transform: rotate(0deg);
				transform: rotate(0deg);
			}
			100% {
				-webkit-transform: rotate(360deg);
				-moz-transform: rotate(360deg);
				-ms-transform: rotate(360deg);
				-o-transform: rotate(360deg);
				transform: rotate(360deg);
			}
		}

		button {
			cursor: pointer;
		}
	</style>

	<body>
		<div class="loading" id="loader">Loading&#8230;</div>

		<br>
		<table class="table table-bordered table-hover reporttable">
			<thead>
				<tr>
					<td style="width:50%">Number of insulin diabetic type 1 patients seen in the reporting period</td>
					<td style="width:50%" id="col1"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of diabetic type 1 patients seen in the reporting period</td>
					<td style="width:50%" id="col2"></td>
				</tr>
				<tr>
					<td style="width:50%">Number of insulin diabetic type 2 patients seen in the reporting period</td>
					<td style="width:50%" id="col3"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of diabetic type 2 patients seen in the reporting period</td>
					<td style="width:50%" id="col4"></td>
				</tr>
				<tr>
					<td style="width:50%">Number of new insulin diabetic type 1 patients seen in the reporting period</td>
					<td style="width:50%" id="col5"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of new diabetic type 1 patients seen in the reporting period</td>
					<td style="width:50%" id="col6"></td>
				</tr>
				<tr>
					<td style="width:50%">Number of new insulin diabetic type 2 patients seen in the reporting period</td>
					<td style="width:50%" id="col7"></td>
				</tr>
				<tr>
					<td style="width:50%">Total number of new diabetic type 2 patients seen in the reporting period</td>
					<td style="width:50%" id="col8"></td>
				</tr>
				<tr>
					<td></td>
				</tr>
				<tr>
					<td style="width:50%">Progress</td>
					<td style="width:50%" id="p"></td>
				</tr>
			</thead>
		</table>
	</body>

</html>